package com.fuhao.workerQueue;

import com.fuhao.utils.RabbitUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 *              |----- C1
 *  P-----Q-----|
 *              |----- c2
 * rabbit模型之工作队列模型：
 *              允许多个消费者
 *              分发模式:
 *                      轮询分发即均分（默认）
 *                      而公平分发即能者多劳
 * @author zhang
 */
public class product {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = RabbitUtils.getConnection();

        Channel channel = connection.createChannel();

        channel.queueDeclare("worker_queue",true,false,false,null);

        int prefetchCount = 1;
        //修改成公平分发
        //每个消费者发送确认信号之前，消息队列不发送下一个消息过来，一次只处理一个消息
        // 限制发给同一个消费者不得超过1条消息
        channel.basicQos(prefetchCount);

        for (int i = 0;i< 20;i++) {
            String msg = "worker queue"+i;
            channel.basicPublish("", "worker_queue", null, msg.getBytes());
            System.out.println("发送消息：" + msg);
        }
        channel.close();
        connection.close();
    }
}
